﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace PBDesk.DAL
{
    public class DBHelper
    {

        #region Methods - Reading value from DataReader

        public static object GetValueFromDataReader<T>(IDataReader reader, string columnName)
        {
            return GetValueFromDataReader<T>(reader, columnName, true, false, default(T));
        }
        public static object GetValueFromDataReader<T>(IDataReader reader, string columnName, bool throwExpIfColNotDefined, bool throwExpIfDBNull, T valueInCaseOfDBNull)
        {
            object retValue = default(T);
            Type returnType = typeof(T);

            if (returnType.ToString() == "System.Boolean")
            {
                retValue = false;
                string tempStr = (string)GetValueFromDataReader<string>(reader, columnName, false, false, "N");
                if (!string.IsNullOrEmpty(tempStr))
                {
                    if (tempStr.Trim().ToUpper().Equals("N") || tempStr.Trim().Equals("0"))
                        retValue = false;
                    else
                        retValue = true;
                }
                return retValue;
            }
            if (returnType.IsEnum)
            {
                retValue = default(T);
                int tempInt = (int)GetValueFromDataReader<int>(reader, columnName);
                if (Enum.IsDefined(returnType, tempInt))
                {
                    retValue = (T)Enum.ToObject(returnType, tempInt);
                }
                return retValue;
            }

            Type readerColumnType = null;
            string errMsg = "Exception at WW.COM.Business.SiteSearch.MarshallHelper.GetValueFromDataReader<T>() :: {0}";
            if (valueInCaseOfDBNull == null)
            {
                valueInCaseOfDBNull = default(T);
            }
            if (reader != null && !string.IsNullOrEmpty(columnName))
            {
                try
                {
                    int columnIndex = reader.GetOrdinal(columnName);
                    if (!reader.IsDBNull(columnIndex))
                    {
                        readerColumnType = reader.GetFieldType(columnIndex);
                        if (readerColumnType == returnType)
                        {
                            switch (returnType.ToString())
                            {
                                case "System.Int64":
                                    {
                                        retValue = reader.GetInt64(columnIndex);
                                        break;
                                    }
                                case "System.Int32":
                                    {
                                        retValue = reader.GetInt32(columnIndex);
                                        break;
                                    }
                                case "System.String":
                                    {
                                        retValue = reader.GetString(columnIndex);
                                        break;
                                    }
                                case "System.DateTime":
                                    {
                                        retValue = reader.GetDateTime(columnIndex);
                                        break;
                                    }
                                case "System.Single":
                                    {
                                        retValue = reader.GetFloat(columnIndex);
                                        break;
                                    }
                                case "System.Double":
                                    {
                                        retValue = reader.GetDouble(columnIndex);
                                        break;
                                    }
                                case "System.Decimal":
                                    {
                                        retValue = reader.GetDecimal(columnIndex);
                                        break;
                                    }
                                default:
                                    {
                                        retValue = Convert.ChangeType(reader.GetValue(columnIndex), returnType);
                                        break;
                                    }
                            }

                        }
                        else
                        {
                            retValue = Convert.ChangeType(reader.GetValue(columnIndex), returnType);

                        }
                    }
                    else
                    {
                        if (throwExpIfDBNull == true)
                        {
                            throw new DataException(string.Format(errMsg, "Column " + columnName + " contains DBNull."));
                        }
                        else
                        {
                            retValue = valueInCaseOfDBNull;
                        }
                    }


                }
                catch (IndexOutOfRangeException indexOutOfRangeEx)
                {
                    if (throwExpIfColNotDefined == true)
                    {
                        throw new DataException(string.Format(errMsg, "Column " + columnName + " not defined in DataReader object"), indexOutOfRangeEx);
                    }
                    else
                    {
                        retValue = valueInCaseOfDBNull;
                    }
                }
                catch (InvalidCastException invalidCastEx)
                {
                    throw new DataException(string.Format(errMsg, "Invalid Casting. Cannot convert " + readerColumnType.ToString() + " to " + returnType.ToString() + " for column " + columnName), invalidCastEx);
                }
            }
            else
            {
                throw new DataException(string.Format(errMsg, "DataReader object is null or ColumnName is null or empty"));
            }
            return retValue;
        }

        #endregion
    }
}
